﻿using Microsoft.Win32;
using System;

namespace DW.Library
{
    public class RegistryHelper
    {
        RegistryKey _registryKey;
        public string _keyPath { get; set; }

        public RegistryHelper(string keyPath)
        {
            _keyPath = keyPath;
            _registryKey = Registry.CurrentUser;
        }


        public RegistryHelper(string keyPath,bool userLocalMachine)
        {
            _keyPath = keyPath;
            if (userLocalMachine)
            {
                _registryKey = Registry.LocalMachine;
            }
            else
            {
                _registryKey = Registry.CurrentUser;
            }
        }

        public bool GetValue(string keyName, out object value)
        {
            value = null;
            try
            {
                if (!string.IsNullOrWhiteSpace(_keyPath))
                {
                    value = _registryKey.OpenSubKey(_keyPath)?.GetValue(keyName);
                }
                return true;
            }
            catch (Exception ex)
            {
            }
            return false;
        }

        public bool GetValue(RegistryView view, RegistryHive hive, string keyName, string valuename, out string value)
        {
            value = null;
            RegistryKey baseKey = null, mainKey = null;
            try
            {
                baseKey = RegistryKey.OpenBaseKey(hive, view);
                if (baseKey != null)
                {
                    mainKey = baseKey.OpenSubKey(keyName);
                    if (mainKey != null)
                    {
                        value = baseKey.GetValue(keyName).ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        public bool GetValue(string keyName, out string value)
        {
            value = null;
            try
            {
                if (!string.IsNullOrWhiteSpace(_keyPath))
                {
                    var registyValue = _registryKey.OpenSubKey(_keyPath)?.GetValue(keyName);
                    if (registyValue != null)
                    {
                        value = registyValue.ToString();
                    }
                    else
                    {
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
            }
            return false;
        }

        public bool SetValue(string keyName, object value, RegistryValueKind registryValueKind = RegistryValueKind.DWord)
        {
            RegistryKey key = null;
            try
            {
                string registryKeyPath = _keyPath;
                key = _registryKey.CreateSubKey(registryKeyPath, true);
                if (key != null)
                {
                    key.SetValue(keyName, value, registryValueKind);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                if (key != null)
                {
                    key.Dispose();
                    key.Close();
                }
            }

            return true;
        }

        public bool DeleteRegistryValueName(string keyName)
        {
            try
            {
                _registryKey.OpenSubKey(_keyPath)?.DeleteValue(keyName, true);
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }
    }
}
